package cn.jiadakeji.parent.service.impl;

import cn.jiadakeji.parent.common.util.StringUtils;
import cn.jiadakeji.parent.domain.School;
import cn.jiadakeji.parent.domain.SchoolClass;
import cn.jiadakeji.parent.domain.Student;
import cn.jiadakeji.parent.dto.SchoolClassDTO;
import cn.jiadakeji.parent.dto.SchoolDTO;
import cn.jiadakeji.parent.dto.StudentDTO;
import cn.jiadakeji.parent.mapper.StudentMapper;
import cn.jiadakeji.parent.service.SchoolClassService;
import cn.jiadakeji.parent.service.SchoolService;
import cn.jiadakeji.parent.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author heyanfeng
 * @create 2022-03-09 15:21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private SchoolClassService classService;


    /**
     * 按照学籍号查找学生信息
     */
    @Override
    public StudentDTO getStudentWithNumber(String number) {
        // 学生信息
        Student student = baseMapper.selectOne(new QueryWrapper<Student>()
                .eq("student_number", number));
        // 类型转换
        return student == null ? null : conversion(student);
    }

    /**
     * 通过班级ID查找学生
     */
    @Override
    public List<Student> getStudentByClassId(String classId) {
        return baseMapper.selectList(new QueryWrapper<Student>()
                .eq("school_class_id", classId));
    }

    /**
     * 通过年级班级查找学生信息
     */
    @Override
    public List<StudentDTO> getStudentWithGrade(String schoolId, String classId) {
        // 用schoolId，查到所有班级classIds
        List<String> classIds = classService.list(new QueryWrapper<SchoolClass>()
                        .eq("school_id", schoolId))
                .stream().map(SchoolClass::getId).collect(Collectors.toList());
        // 用classIds查找所有的学生
        // TODO:待测试
        List<Student> studentList = null;
        for (String id : classIds) {
            studentList = baseMapper.getStudentByClassId(id);
        }
        // 转换类型
        return studentList.stream().map(student -> {
            return conversion(student);
        }).collect(Collectors.toList());

    }

    @Override
    public StudentDTO getStudentById(String studentId) {
        // 学生信息
        Student student = baseMapper.selectById(studentId);
        // 类型转换
        return student == null ? null : conversion(student);

    }

    /**
     * 类型转换
     * 将student对象转换为StudentDTO
     */
    @NotNull
    private StudentDTO conversion(Student student) {
        StudentDTO studentDTO = new StudentDTO();
        BeanUtils.copyProperties(student, studentDTO);
        // 设置出生日期
        studentDTO.setBirthDate(student.getBirthdate());
        // 设置班级信息
        SchoolClassDTO schoolClassDTO = getSchoolClassDTO(student);
        studentDTO.setSchoolClassDTO(schoolClassDTO);
        // 设置学校信息
        SchoolDTO schoolDTO = getSchoolDTO(schoolClassDTO.getSchoolId());
        studentDTO.setSchoolDTO(schoolDTO);
        return studentDTO;
    }

    /**
     * 获取SchoolDTO
     *
     * @param schoolId 学校ID
     */
    @NotNull
    public SchoolDTO getSchoolDTO(String schoolId) {
        School school = schoolService.getById(schoolId);
        SchoolDTO schoolDTO = new SchoolDTO();
        BeanUtils.copyProperties(school, schoolDTO);
        return schoolDTO;
    }

    /**
     * 获取SchoolClassDTO
     *
     * @param student 学生对象
     */
    @NotNull
    public SchoolClassDTO getSchoolClassDTO(Student student) {
        SchoolClass schoolClass = classService.getById(student.getSchoolClassId());
        SchoolClassDTO schoolClassDTO = new SchoolClassDTO();
        BeanUtils.copyProperties(schoolClass, schoolClassDTO);
        return schoolClassDTO;
    }


}
